package com.cpi.cornpp.purchase.program.action;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.biweek.exception.BiWeekException;
import com.cpi.cornpp.biweeks.services.BiWeekService;
import com.cpi.cornpp.corntypes.services.CornTypesService;
import com.cpi.cornpp.plants.services.PlantService;
import com.cpi.cornpp.plants.view.PlantView;
import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.purchase.program.form.PurchaseOrderForm;
import com.cpi.cornpp.purchase.program.services.PurchaseProgramService;
import com.cpi.cornpp.purchase.program.view.PurchaseOrderView;
import com.cpi.cornpp.transports.exception.TransportException;
import com.cpi.cornpp.transports.service.TransportService;
import com.cpi.cornpp.transports.view.TransportView;

public class ShowEditPurchaseOrderAction extends Action {
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	private static final String SUCCESS_FORWARD = "toSuccess";//$NON-NLS-1$
	private static final String FAILURE_FORWARD = "error";//$NON-NLS-1$
	private static final String PURCHASE_ORDER_ID = "purchaseOrderId";//$NON-NLS-1$
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws PurchaseProgramException{
		String messageError = null;
		String purchaseOrderId = null;
		PlantView plantView = null;
		PurchaseOrderForm purchaseOrderForm = null;
		PurchaseOrderView purchaseOrderView = null;
		TransportView transportView = null;
		PlantService plantService = null;
		BiWeekService biWeekService = null;
		TransportService transportService = null;
		CornTypesService cornTypesService = null;
		PurchaseProgramService purchaseProgramService = null;
		List plants = null;
		List purchaseDates = null;
		List costElements = null;
		List cornTypes = null;
		HttpSession session = null;
		ActionForward forward = null;
		MessageResources messageResources = null;
		
		session = request.getSession(false);
		messageResources = getResources(request);
		forward = mapping.findForward(FAILURE_FORWARD);
		
		try{
			
			if (form != null){
				purchaseOrderForm = (PurchaseOrderForm) form;
				
				purchaseOrderId = purchaseOrderForm.getPurchaseOrderId();
				
				if (StringUtils.isEmpty(purchaseOrderId)){
					purchaseOrderId = (String)session.getAttribute(PURCHASE_ORDER_ID);
				}
				plantView = new PlantView();
				plantService = new PlantService();
				biWeekService = new BiWeekService();
				transportService = new TransportService();
				purchaseProgramService = new PurchaseProgramService();
				cornTypesService = new CornTypesService();
				
				purchaseOrderView = purchaseProgramService.getPurchaseOrderById(Integer.parseInt(purchaseOrderId));
				transportView = transportService.getTransport(Integer.parseInt(purchaseOrderView.getPurchaseOrderTransportId()));
				plants = plantService.list();
				costElements = transportService.getTransportCostElements(Integer.parseInt(purchaseOrderView.getPurchaseOrderTransportId()));
				purchaseDates = biWeekService.getBiWeeks();
				cornTypes = cornTypesService.getListCornTypes();
				
				plantView.setPlantId(Integer.parseInt(purchaseOrderView.getPurchaseOrderPlantId()));
				plantView = plantService.getPlant(plantView);
				purchaseOrderView.setPurchaseTransportView(transportView);
				purchaseOrderView.setPurchaseOrderPlantView(plantView);
				
				
				request.setAttribute("purchaseOrderView", purchaseOrderView);
				request.setAttribute("transportCostElements", costElements);
				request.setAttribute("purchaseDates", purchaseDates);
				request.setAttribute("plants", plants);
				request.setAttribute("cornTypes", cornTypes);
				
				forward = mapping.findForward(SUCCESS_FORWARD);
			}
			
		} catch(PurchaseProgramException ppe){
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), ppe.getErrorCode());
			
			if(null == messageError){
				session.setAttribute(CUSTOM_EXCEPTION, ppe);
				form.reset(mapping, request);
				throw ppe;
			}else{
				session.setAttribute(ERROR_MESSAGE, messageError);
				forward = mapping.findForward(FAILURE_FORWARD);
			}
		} catch(TransportException tre){
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), tre.getErrorCode());
			
			if(null == messageError){
				PurchaseProgramException ppe = new PurchaseProgramException(tre, PurchaseProgramException.LAYER_ACTION, PurchaseProgramException.ACTION_LISTS);
				session.setAttribute(CUSTOM_EXCEPTION, ppe);
				form.reset(mapping, request);
				throw ppe;
			}else{
				session.setAttribute(ERROR_MESSAGE, messageError);
				forward = mapping.findForward(FAILURE_FORWARD);
			}
		} catch(BiWeekException bwe){
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), bwe.getErrorCode());
			
			if(null == messageError){
				PurchaseProgramException ppe = new PurchaseProgramException(bwe, PurchaseProgramException.LAYER_ACTION, PurchaseProgramException.ACTION_LISTS);
				session.setAttribute(CUSTOM_EXCEPTION, ppe);
				form.reset(mapping, request);
				throw ppe;
			}else{
				session.setAttribute(ERROR_MESSAGE, messageError);
				forward = mapping.findForward(FAILURE_FORWARD);
			}
		} catch(Exception e){
			PurchaseProgramException ppe = new PurchaseProgramException(e , TransportException.LAYER_ACTION, TransportException.ACTION_LISTS);;
			
			session.setAttribute(CUSTOM_EXCEPTION, ppe);
			form.reset(mapping, request);
			e.printStackTrace(System.out);
			
			throw ppe;
		}
		
		return forward;
	}
}